﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace RonYee.App_Code
{
    DBConnection myConn=new DBConnection();

    public class DBHelper
    {

    }

#region  ///执行SQL语句，返回值为int型：成功返1，失败返回0
    public int sqlEx(string str)
    {
        SqlConnection conn=myConn.GetConnection();
        conn.Open();
        SqlCommand cmd=new SqlCommand(str,conn);
        try
        {
            cmd.ExecuteNonQuery();
            return 1;
        }
        catch (System.Exception ex)
        {
            return 0;
        }
        finally
        {
            conn.Dispose();
        }
    }
#endregion

#region ///执行SQL语句，无返回值

    public void DoSql(string mysql) 
    {
        SqlConnection conn = myConn.GetConnection();
        conn.Open();
        SqlCommand cmd = new SqlCommand(mysql, conn);
        cmd.ExecuteNonQuery(); 
        conn.Close();
    }
#endregion

#region  ///执行SQL查询语句,返回DataTable数据表

    public DataTable reDt(string cmdstr)
    {
        SqlConnection con = myConn.GetConnection();
        SqlDataAdapter da = new SqlDataAdapter(cmdstr, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        return (ds.Tables[0]);
    }

#endregion
 
#region /// 执行SQL查询语句,返回SqlDataReader对象dr

    public SqlDataReader reDr(string str)
    {
        SqlConnection conn = myConn.GetConnection();
        conn.Open();
        SqlCommand com = new SqlCommand(str, conn);
        SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection);
        conn.Close();
        return dr;   
    }
#endregion

#region ///执行SQL查询语句，传递SqlStr与TableName两个对象，返回DataSet表对象

    public System.Data.DataSet GetDataSet(string sqlStr, string TableName)
    {
        SqlConnection conn = myConn.GetConnection();
        conn.Open();
        SqlDataAdapter adapt = new SqlDataAdapter(sqlStr, conn);
        DataSet ds = new DataSet();
        adapt.Fill(ds, TableName);
        conn.Close();
        return ds;
    }
#endregion

#region ///执行SQL查询语句，传递SqlStr对象，返回DataSet表对象

    public System.Data.DataSet GreatDs(string sql)
    {
        SqlConnection myconn = myConn.GetConnection();
        myconn.Open();
        SqlDataAdapter mydr = new SqlDataAdapter(sql, myconn);
        DataSet myds = new DataSet();
        mydr.Fill(myds);
        myconn.Close();
        return myds;
    }
 
#endregion
 
#region ///执行SQL查询语句，传递UserName对象，返回True or Flase;

    public static bool isExists(string Name)
    {
        string sql = "select * from Users where LoginID='" + Name + "'";
        SqlConnection conn = myConn.GetConnection();
        conn.Open(); 
        SqlCommand comm = new SqlCommand(sql, conn);  
        SqlDataReader rs = comm.ExecuteReader(); 
        if (rs.Read())
        {
            return true;
        }
        rs.Close();
        return false;

    }
 
#endregion

#region //加密密码,md5加密方法

    public static string mymd5(string str) 
    {
        
        string EnPswstr = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
        return (EnPswstr);
    }
#endregion
 
   

}